﻿#include "include/Game/Terrain.h"
#include <fstream>
void Terrain::load(const char* mapFilePath)//根据地图文件生成地形
{
    std::ifstream in(mapFilePath);
    int n;
    in>>n;//第一行一个整数n表示地图大小
    if(n!=MAP_SIZE)
    {
        //抛出错误
    }
    for(int x=0;x<MAP_SIZE;x++)
        for(int y=0;y<MAP_SIZE;y++)
        {
            int typeId;
            in>>typeId;
            type[x][y]=Terrain::gridType(typeId);
        }
    for(int x=0;x<MAP_SIZE;x++)
        for(int y=0;y<MAP_SIZE;y++)
            in>>energy[x][y];
    in.close();
}

Terrain::Terrain() {
    type.resize(MAP_SIZE);
    energy.resize(MAP_SIZE);
    for (int i = 0; i < MAP_SIZE; ++ i) {
        type[i].resize(MAP_SIZE);
        energy[i].resize(MAP_SIZE);
        for (int j = 0; j < MAP_SIZE; ++j) {
            type[i][j] = MAP[i][j] ? EMPTY : OBSTACLE;
            energy[i][j] = ENERGY[i][j];
        }
    }
}

Terrain::gridType Terrain::getType(int _x, int _y) {
    if (_x < 0 || MAP_SIZE <= _x) return OBSTACLE;
    if (_y < 0 || MAP_SIZE <= _y) return OBSTACLE;
    return type[_x][_y];
}
